/*  Copyright (c) 2012 William Rogers and James Boud

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.ComponentModel;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

using TInput = Microsoft.Xna.Framework.Content.Pipeline.Graphics.TextureContent;
using TOutput = JBBRXG11ContentTexture.Texture2DInfoStuff;

namespace JBBRXG11ContentTexture
{
    [ContentProcessor(DisplayName = "JBBRXG11 Texture2D Processor")]
    public class JBBRXG11Texture2DContentProcessor : ContentProcessor<TInput, TOutput>
    {
        #region Processor Parameters
        // For the processor attributes to work need "using System.ComponentModel"
        Color colourkey = new Color(255, 0, 255, 255);
        bool colourkeyenabled = false;
        bool premultiplyalpha = true;
        bool generatemipmaps = false;
        bool resizetopoweroftwo = false;
        //public enum mycolours { red, blue, green, purple };
        //mycolours mycolour;

        [DisplayName("Color Key Color")]
        [DefaultValue("255, 0, 255, 255")]
        public Color ColorKeyColor { get { return colourkey; } set { colourkey = value; } }

        [DisplayName("Colour Key Enabled")]
        [DefaultValue("True")]
        public bool ColorKeyEnabled { get { return colourkeyenabled; } set { colourkeyenabled = value; } }

        [DisplayName("Premultiply Alpha")]
        [DefaultValue("True")]
        public bool PremultiplyAlpha { get { return premultiplyalpha; } set { premultiplyalpha = value; } }

        [DisplayName("Generate Mipmaps")]
        [DefaultValue("False")]
        public bool GenerateMipmaps { get { return generatemipmaps; } set { generatemipmaps = value; } }

        [DisplayName("Resize to Power of Two")]
        [DefaultValue("False")]
        public bool ResizeToPowerOfTwo { get { return resizetopoweroftwo; } set { resizetopoweroftwo = value; } }

        //[DisplayName("Sample enum value")]
        //[DefaultValue("red")]
        //public mycolours MyColour { get { return mycolour; } set { mycolour = value; } }
        #endregion Processor Parameters

        public override TOutput Process(TInput input, ContentProcessorContext context)
        {
            Texture2DInfoStuff tis = new Texture2DInfoStuff();

            TextureProcessor texproc = new TextureProcessor();
            texproc.ColorKeyColor = colourkey;
            texproc.ColorKeyEnabled = colourkeyenabled;
            texproc.GenerateMipmaps = generatemipmaps;
            texproc.PremultiplyAlpha = premultiplyalpha;
            texproc.ResizeToPowerOfTwo = resizetopoweroftwo;
            TextureContent output = texproc.Process(input, context);
            Texture2DContent outcont = output as Texture2DContent;

            if (outcont.Faces.Count != 1)
                throw new Exception("JBBRXG11(NYI): Can't deal with textures that have more than one face");

            foreach (MipmapChain chain in outcont.Faces)  // only one at the moment
            {
                int m;
                byte[] bmpixdata;

                tis.mipchain = new TextureBitmap[chain.Count];

                m = 0;
                foreach (BitmapContent bm in chain)
                {
                    SurfaceFormat format;// = SurfaceFormat..Unknown;
                    bm.TryGetFormat(out format);
                    tis.mipchain[m].width = bm.Width;
                    tis.mipchain[m].height = bm.Height;
                    tis.mipchain[m].format = format;
                    // Swap bgra to rgba - hope other formats don't need this
                    //tis.mipchain[m].data = new byte[bm.Width * bm.Height * 4];
                    bmpixdata = bm.GetPixelData();
                    /*if (format == SurfaceFormat.Color)
                    {
                        for (int p = 0; p < bmpixdata.Length; p += 4)
                        {
                            byte t;
                            t = bmpixdata[p];
                            bmpixdata[p] = bmpixdata[p + 2];
                            bmpixdata[p + 2] = t;
                            //bmpixdata[p] = 0;//255;
                           // bmpixdata[p + 1] = t;
                            //bmpixdata[p + 2] = 0;
                            bmpixdata[p + 3] = 255; 
                        }
                    }*/
                    tis.mipchain[m].data = bmpixdata;
                    m++;
                }
            }
            return tis;
        }
    }
}
